package com.palm.hubo2o.adapter;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.palm.hubo2o.R;
import com.palm.palmutils.widget.BallView;

/**
 * 类似百十位选号列表
 * Created by dest1 on 2015/10/15.
 */
public abstract class DigitBallAdapter extends RecyclerView.Adapter<DigitBallAdapter.DigitBallHolder> {
    public static final int ITEM_TYPE = 0;
    public static final int MARK_TYPE = 1;
    public static final int DIGIT_TYPE = 2;
    private int mRow;

    public void setRowCount(int row) {
        mRow = row;
    }


    @Override
    public DigitBallHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        DigitBallHolder holder;
        if (viewType == MARK_TYPE) {
            holder = onCreateLabelHolder(parent);
            holder.itemView.setEnabled(false);
            holder.setType(MARK_TYPE);
        } else if (viewType == DIGIT_TYPE) {
            holder = onCreateDigitHolder(parent);
            holder.itemView.setEnabled(false);
            holder.setType(DIGIT_TYPE);
        } else {
            BallView ball = new BallView(parent.getContext());
            holder = new DigitBallHolder(ball);
            holder.setType(ITEM_TYPE);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(DigitBallHolder holder, int position) {
        int type = holder.getType();
        if (type == MARK_TYPE) {
            onBindLabelViewHolder(holder, position / 12);
        } else if (type == DIGIT_TYPE) {
            onBindDigitViewHolder(holder, position / 12);
        } else {
            BallView ball = (BallView) holder.itemView;
            ball.setBallColor(ContextCompat.getColor(ball.getContext(), R.color.red));
            ball.setNumber(String.valueOf(position % 12 - 2));
        }
    }

    @Override
    public int getItemCount() {
        return (10 + 2) * mRow;
    }

    @Override
    public int getItemViewType(int position) {
        int res = position % 12;
        if (res == 0)
            return MARK_TYPE;
        if (res == 1)
            return DIGIT_TYPE;
        return ITEM_TYPE;
    }

    /**
     * 转换成原始位置
     *
     * @return
     */
    public int transToRawPosition(int posInGroup, int groupIndex) {
        return posInGroup + groupIndex * 12 + 2;
    }

    public abstract DigitBallHolder onCreateDigitHolder(ViewGroup parent);

    public abstract DigitBallHolder onCreateLabelHolder(ViewGroup parent);

    public abstract void onBindDigitViewHolder(DigitBallHolder holder, int groupIndex);

    public abstract void onBindLabelViewHolder(DigitBallHolder holder, int groupIndex);

    public class DigitBallHolder extends RecyclerView.ViewHolder {
        private int type;

        public DigitBallHolder(View itemView) {
            super(itemView);
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }
    }
}
